package com.production.CoursePlatform.controller;

import com.production.CoursePlatform.entity.Submission;
import com.production.CoursePlatform.service.SubmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/submission")
public class SubmissionController {
    //此接口已检验
    @Autowired
    private SubmissionService submissionService;

    // 创建新提交
    @PostMapping("/create")
    public ResponseEntity<String> createSubmission(@RequestBody Submission submission) {
        int result = submissionService.createSubmission(submission);
        if (result > 0) {
            return ResponseEntity.ok("提交记录创建成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("提交记录创建失败");
        }
    }

    // 按 ahID 查询提交
    @GetMapping("/findByAhid/{ahID}")
    public ResponseEntity<List<Submission>> findByAhID(@PathVariable int ahID) {
        List<Submission> submissions = submissionService.findByAhID(ahID);
        return ResponseEntity.ok(submissions);
    }

    // 删除提交
    @DeleteMapping("/delete/{shID}")
    public ResponseEntity<String> deleteSubmission(@PathVariable int shID) {
        int result = submissionService.deleteSubmission(shID);
        if (result > 0) {
            return ResponseEntity.ok("提交记录删除成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("提交记录删除失败");
        }
    }

    // 更新提交记录
    @PutMapping("/update")
    public ResponseEntity<String> updateSubmission(@RequestBody Submission submission) {
        int result = submissionService.updateSubmission(submission);
        if (result > 0) {
            return ResponseEntity.ok("提交记录更新成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("提交记录更新失败");
        }
    }
}

